package com.bwie.service.impl;

import com.bwie.common.domain.Login;
import com.bwie.common.domain.Shop;
import com.bwie.common.domain.vo.ShopVo;
import com.bwie.mapper.LoginMapper;
import com.bwie.mapper.ShopMapper;
import com.bwie.service.ShopService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @Author：Minxiao Hao
 * @Description:
 * @name：ShopServiceImpl
 * @Date：2024/11/4 10:40
 */
@Service
public class ShopServiceImpl implements ShopService {
    @Autowired
    private ShopMapper shopMapper;
    @Autowired
    private LoginMapper loginMapper;

    /**
     * 分页 每页展示 3条数据
     * @param shopVo
     * @return
     */
    @Override
    public List<ShopVo> findShopList(ShopVo shopVo) {
        List<ShopVo> list = shopMapper.findShopList(shopVo);
        return list;
    }

    /**
     * 添加 购买商品
     * @param shop
     */
    @Override
    @Transactional
    public void insertShop(Shop shop) {
        //品数量允许编辑，设置最小值为1，最大值为99，
        // 并进行相应的表单校验
        for (int i = 1; i < 99; i++) {
            shop.setShopCount(i);
        }
        //商品名称、价格及总计金额字段设置为不可编辑状态
        if (shop.getShopCount()!=0 &&shop.getShopPrice()>0){
            int a = shop.getShopPrice()*shop.getShopCount();
            shop.setShopSum(a);
        }
        //购买成功后，根据规则回馈用户积分，即每消费10元累计1积分
        int s = shop.getShopSum()/10;
        Login login = new Login();
        login.setLoginCount(s);

        loginMapper.insertLogin(login.getLoginCount());

        shopMapper.insertShop(shop);
    }
}
